package com.broada.apm.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.transaction.annotation.Transactional;

import com.broada.apm.dao.ApplicationDao;
import com.broada.apm.model.ApplicationSystem;

public class MysqlApplicationDao extends MysqlAbstractDao implements ApplicationDao {

	public static final String APPLICATION_NAMESPACE = "com.broada.apm.dao.ApplicationDao.";
	
	@Override
	public void addApplication(ApplicationSystem application) {
		getSqlSession().insert(APPLICATION_NAMESPACE + "addApplication", application);
		if (application.getApplicationIds() != null && application.getApplicationIds().length > 0)
			getSqlSession().insert(APPLICATION_NAMESPACE + "addSystemApplicationMap", application);
		if (application.getRelationIds() != null && application.getRelationIds().length > 0)
			getSqlSession().insert(APPLICATION_NAMESPACE + "addSystemRelationMap", application);
	}

	@Override
	public void updateApplication(ApplicationSystem application) {
		getSqlSession().update(APPLICATION_NAMESPACE + "updateApplication", application);
	}

	@Override
	@Transactional
	public void updateApplicationWithRelation(ApplicationSystem application) {
		updateApplication(application);
		
		getSqlSession().delete(APPLICATION_NAMESPACE + "deleteAppServiceMap", application);
		getSqlSession().delete(APPLICATION_NAMESPACE + "deleteAppRelationMap", application);
		
		if(application.getApplicationIds() != null && application.getApplicationIds().length > 0)
			getSqlSession().insert(APPLICATION_NAMESPACE + "addSystemApplicationMap", application);
		if(application.getRelationIds() != null && application.getRelationIds().length > 0)
			getSqlSession().insert(APPLICATION_NAMESPACE + "addSystemRelationMap", application);
	}

	@Override
	@Transactional
	public void deleteApplication(ApplicationSystem application) {
		getSqlSession().delete(APPLICATION_NAMESPACE + "deleteApplication", application);
		getSqlSession().delete(APPLICATION_NAMESPACE + "deleteAppServiceMap", application);
		getSqlSession().delete(APPLICATION_NAMESPACE + "deleteAppRelationMap", application);
	}

	@Override
	public List<ApplicationSystem> getAllApplications() {
		return getSqlSession().selectList(APPLICATION_NAMESPACE + "getAllApplications");
	}

	@Override
	public ApplicationSystem getApplicationById(String appId) {
		return getSqlSession().selectOne(APPLICATION_NAMESPACE + "getApplicationById", appId);
	}

	@Override
	public ApplicationSystem getApplicationWithServiceAndRelById(String appId) {
		ApplicationSystem app = getApplicationById(appId);
		List<String> applicationIds = getSqlSession().selectList(APPLICATION_NAMESPACE + "getApplicationIdsBySystemId", appId);
		List<String> relationIds = getSqlSession().selectList(APPLICATION_NAMESPACE + "getRelationIdsBySystemId", appId);
		if (app != null) {
			app.setApplicationIds(applicationIds.toArray(new String[applicationIds.size()]));
			app.setRelationIds(relationIds.toArray(new String[relationIds.size()]));
		}
		return app;
	}

	@Override
	public ApplicationSystem getApplicationByName(String appName) {
		return getSqlSession().selectOne(APPLICATION_NAMESPACE + "getApplicationByName", appName);
	}

	@Override
	public List<ApplicationSystem> getApplicationByCondition(ApplicationSystem application) {
		return getSqlSession().selectList(APPLICATION_NAMESPACE + "getApplicationByCondition", application);
	}

	@Override
	public void saveAppsInOverview(List<String> appIds) {
		Map<String, List<String>> map = new HashMap<String, List<String>>();
		if (appIds.isEmpty()) {
			map.put("appIds", null);
			getSqlSession().selectOne(APPLICATION_NAMESPACE + "deleteAppsInOverview", map);
		} else {
			map.put("appIds", appIds);
			getSqlSession().selectOne(APPLICATION_NAMESPACE + "deleteAppsInOverview", map);
			getSqlSession().selectOne(APPLICATION_NAMESPACE + "saveAppsInOverview", map);
		}
	}

	@Override
	public List<ApplicationSystem> getApplicationsByNames(String[] appNames) {
		return getSqlSession().selectList(APPLICATION_NAMESPACE + "getApplicationsByNames", appNames);
	}

	@Override
	public List<ApplicationSystem> getApplicationsByIds(String[] appIds) {
		return getSqlSession().selectList(APPLICATION_NAMESPACE + "getApplicationsByIds", appIds);
	}

}
